package com.ima;

import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.dev.CheckableItem;
import com.dev.ColorTextButton;
import com.dev.GUI;
import com.dev.IconViewClose;
import com.dev.MainActivity;
import com.dev.Search;
import com.dev.ShowSoftInput;
import com.dev.UnderlineSingleLineEditor;

class QueryForMagic extends LinearLayout implements OnClickListener,OnEditorActionListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	IconViewClose close;
	TextView title;
	LinearLayout row1;
	TextView row1_text;
	ColorTextButton row1_bt;
	Search.IV2Bar add,sub;
	CheckableItem itAngle;
	CheckableItem itSeries;
	UnderlineSingleLineEditor editor;
	ShowSoftInput ssi;
	MainActivity ma;
	QueryForMagic(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new GUI.BorderDrawable());
		setFocusable(true);
		setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
		int textSize=(int)(13*ma.dp);
		int padding=9;
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		ssi=new ShowSoftInput(ma);
		ssi.setOnClickListener(this);
		title=new TextView(ma);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.getPaint().setTextSize(textSize);
		title.setPadding(padding,0,0,0);
		title.setText("魔棒工具选项：");
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.setBackground(new GUI.BorderDrawable());
		titlebar.addView(title,new LayoutParams(0,-1,1f));
		titlebar.addView(ssi);
		titlebar.addView(close);
		sub=new Search.IV2Bar(ma);
		sub.setOnClickListener(this);
		add=new Search.IV2Bar(ma);
		add.setOnClickListener(this);
		add.verBar=true;
		editor=new UnderlineSingleLineEditor(ma);
		editor.setPadding(0,0,0,0);
		editor.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		editor.setOnEditorActionListener(this);
		row1_text=new TextView(ma);
		row1_text.setGravity(Gravity.CENTER_VERTICAL);
		row1_text.setPadding(padding,0,0,0);
		row1_text.setTypeface(Typeface.MONOSPACE);
		row1_text.setText("误差（0~255）");
		row1_bt=new ColorTextButton(ma);
		row1_bt.setText("重设");
		row1_bt.setOnClickListener(this);
		row1=new LinearLayout(ma);
		row1.setOrientation(HORIZONTAL);
		row1.addView(row1_text,-2,-1);
		row1.addView(editor,new LayoutParams(0,-1,1f));
		row1.addView(sub);
		row1.addView(row1_bt);
		row1.addView(add);
		itAngle=new CheckableItem(ma,"生长斜向四角方向的像素");
		itAngle.setOnClickListener(this);
		itAngle.border=false;
		itSeries=new CheckableItem(ma,"连续选择");
		itSeries.setOnClickListener(this);
		itSeries.border=false;
		LayoutParams rowlp=new LayoutParams(-1,(int)(24*ma.dp));
		addView(titlebar,-1,ma.dialogTitleBarHeight);
		addView(row1,rowlp);
		addView(itAngle,rowlp);
		addView(itSeries,rowlp);
	}
	ImageManipulationPage imp;
	void show(ImageManipulationPage imp){
		this.imp=imp;
		editor.setText(Integer.toString(imp.cv.deviation));
		itAngle.setSelected(imp.cv.growAngle);
		if(imp.growSeries){
			itSeries.setSelected(true);
			itAngle.setEnabled(true);
		}else{
			itSeries.setSelected(false);
			itAngle.setEnabled(false);
		}

		wlp.width=ma.wp*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		String usrinput=editor.getText().toString();
		try{
			int n=Integer.parseInt(usrinput);
			if(n>=0){
				if(n<255){
					imp.cv.deviation=n;
					ma.wm.removeView(this);
				}
			}else{
				Toast.makeText(ma,"误差值必须>=0",0).show();
			}
		}catch(NumberFormatException nfe){
			editor.requestFocus();
			Toast.makeText(ma,"无效的误差值",0).show();
		}
	}
	@Override
	protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
		return true;
	}

	@Override
	public boolean onEditorAction(TextView textView, int p, KeyEvent keyEvent) {
		if(textView==editor){
			String ui=editor.getText().toString();
			try{
				int n=Integer.parseInt(ui);
				if(n>=0&&n<255){
					imp.cv.deviation=n;
					editor.clearFocus();
				}else{
					Toast.makeText(ma,"无效的数值",0);
					return true;
				}
			}catch(Exception e){
				Toast.makeText(ma,"无法识别输入内容",0).show();
				return true;
			}
		}
		return false;
	}
	@Override
	public void onClick(View view) {
		if(view==close){
			dismiss();
		}else if(view==add){
			imp.cv.deviation++;
			if(imp.cv.deviation>255){
				imp.cv.deviation=255;
			}else if(imp.cv.deviation<0){
				imp.cv.deviation=0;
			}
			editor.setText(Integer.toString(imp.cv.deviation));
		}else if(view==sub){
			imp.cv.deviation--;
			if(imp.cv.deviation>255){
				imp.cv.deviation=255;
			}else if(imp.cv.deviation<0){
				imp.cv.deviation=0;
			}
			editor.setText(Integer.toString(imp.cv.deviation));
		}else if(view==row1_bt){
			imp.cv.deviation=0;
			editor.setText(Integer.toString(imp.cv.deviation));
		}else if(view==itAngle){
			if(itAngle.isSelected()){
				itAngle.setSelected(false);
				imp.cv.growAngle=false;
			}else{
				itAngle.setSelected(true);
				imp.cv.growAngle=true;
			}
		}else if(view==ssi){
			if(editor.requestFocus()){
				ma.imm.showSoftInput(editor,0);
				editor.selectAll();
			}
		}else if(view==itSeries){
			if(itSeries.isSelected()){
				itSeries.setSelected(false);
				itAngle.setEnabled(false);
				imp.growSeries=false;
			}else{
				itSeries.setSelected(true);
				itAngle.setEnabled(true);
				imp.growSeries=true;
			}
		}
	}
}
